//
//  Shebei_guanli_vc.m
//  ainkjj
//
//  Created by zhiyiLiu on 2020/3/4.
//  Copyright © 2020 zhiyiLiu. All rights reserved.
//

#import "Shebei_guanli_vc.h"
#import "Shebei_guanli_CvCell.h"
#import "App_devices_model.h"

@interface Shebei_guanli_vc ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (weak, nonatomic) IBOutlet UILabel *flgTip_lb;
@property (weak, nonatomic) IBOutlet UIView *flagV;
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;


@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, assign)CGFloat itemWidth;
@property (nonatomic, assign)CGFloat itemHeight;

@end

@implementation Shebei_guanli_vc

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    // /api/App/devices
    
    // Authorization=wq56wqbCrcKxwqp6w519wqV0w5t9fMKXZsKBwrJ5acKCwojCksOcwrLCucKIwq_Ch8ONwpLCkcKqwrDCvmbCscOTwofDkn3CtcKIwpV8wozChsKgwpLCpcKJwqnCgsKIf8OPwr7CuGTCnsKSwpXChsKTwq3CisKmwq3CscK6wpLDoMKKwrV1w41-fHlowobCiXFy&app_code=ainankang
    
    
    /*
     {
     "error_code":0,
     "error_msg":"操作成功",
     "data":[
     
     ]
     }
     */
    self.title = @"设备管理";
    self.dataArr = [NSMutableArray array];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    CGFloat padding = 10.0f;
    flowLayout.minimumInteritemSpacing = 1 ; //两个元素之间的最小间距
    
    flowLayout.minimumLineSpacing =   padding ; //行间距 最小间距
//        flowLayout.sectionInset = UIEdgeInsetsMake(padding * 0.1, padding ,padding  , padding ); //设置item的边距
    
    CGFloat w =  (ScreenWidth - padding * 2 ) / 5.0;
    self.itemWidth = w ;
    
    CGFloat h = 122 * self.itemWidth / 140.0;
    self.itemHeight = h + 40;
    
    
    self.collectionView.collectionViewLayout = flowLayout;

    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;

    // 注册CvCell
    [self.collectionView registerClass:[Shebei_guanli_CvCell class] forCellWithReuseIdentifier:@"Shebei_guanli_CvCell"];

    
    [self req];
    
}
- (void)req{

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:app_code forKey:@"app_code"];
    [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"Authorization"];
    
    [PPHTTPRequest req:@"api/App/devices" parameters:dict success:^(id response) {
        NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
        if ([error_code isEqualToString:@"0"]) {
            App_devices_model *data = [App_devices_model mj_objectWithKeyValues:response];
            if (data.data.count > 0) {
                self.dataArr = data.data.mutableCopy;
                [self.collectionView reloadData];
                self.flagV.hidden = NO;
                self.flgTip_lb.hidden = NO;
                
                [self hideEmptyView];
            }else{
                
                self.flgTip_lb.hidden = YES;
                self.flagV.hidden = YES;
                [self showEmptyViewWithImage:UIImageMake(@"notime_img") text:nil detailText:nil buttonTitle:nil buttonAction:NULL];

            }
        }
        
        else
            //            if ([error_code isEqualToString:@""])
        {
            
            [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
            
        }
    } failure:^(NSError *error) {
        [MBProgressHUD showTipMessageInView:[error localizedDescription]];
        
    }];
}
#pragma mark ====== UICollectionViewDelegate ======
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    Shebei_guanli_CvCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Shebei_guanli_CvCell" forIndexPath:indexPath];
    if (!cell) {
        cell = [[Shebei_guanli_CvCell alloc]init];
    }
    App_devicesData *aDevice = [self.dataArr objectAtIndex:indexPath.row];
    
    cell.idx = indexPath.row;
    cell.entity = aDevice;
    cell.del_blk = ^(NSUInteger idx) {

        App_devicesData *theDevice = [self.dataArr objectAtIndex:idx];

        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:theDevice.device_id forKey:@"device_id"];
        [dict setObject:AuthSignin_model.sharedInstance.data.token forKey:@"Authorization"];

        [PPHTTPRequest req:@"api/App/deactive" parameters:dict success:^(id response) {
            NSString *error_code = [NSString stringWithFormat:@"%@", response[@"error_code"]];
            if ([error_code isEqualToString:@"0"]) {
                
                [self.dataArr removeObjectAtIndex:idx];
                [self.collectionView reloadData];
                
            }
            
            else
                //            if ([error_code isEqualToString:@""])
            {
                
                [MBProgressHUD showTipMessageInView:response[@"error_msg"]];
                
            }
        } failure:^(NSError *error) {
            [MBProgressHUD showTipMessageInView:[error localizedDescription]];
            
        }];
    
//
        // device_id
        /*
         {
         "error_code": 0,
         "error_msg": '操作成功',
         "data": []
         }
         */
    };
    
    
    
    return cell;
    
    
}


//跳转到详情页面
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.itemWidth, self.itemHeight);
}

#pragma mark - 无数据占位

#pragma mark - DZNEmptyDataSetDelegate Methods
//

//- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view
//{
//    [self req];
//}
////- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
////{
////
////
////    [self loadRefresh];
////}
//
//
//- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
//{
//    return [UIImage imageNamed:@"notime_img"];
//
//}
//- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
//{
//    NSString *text = @"暂无数据，点击刷新";
//    UIFont *font = [UIFont boldSystemFontOfSize:15.0];
//    UIColor *textColor = themeColor;
//
//    NSMutableDictionary *attributes = [NSMutableDictionary new];
//
//
//    if (!text) {
//        return nil;
//    }
//
//    if (font) [attributes setObject:font forKey:NSFontAttributeName];
//    if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
//
//    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
//
//}


@end
